<?php
namespace dreamphp\core;

class View
{
	private $module = null;
	private $controller = null;
	private $action =null;
	private $variables = [];
	
	public function __construct($module, $controller, $action)
	{
		$this->module = $module;
		$this->controller = strtolower($controller);
		$this->action = $action;
	}
	
	public function assign($viewData)
	{
		if (is_array($viewData)) {
			foreach ($viewData as $key => $value) {
				$this->variables[$key] = $value;
			}
		}
	}
	
	public function fetch($view = null)
	{
		extract($this->variables);
		$view = empty($view) ? $this->action : strtolower($view);
		$viewFile = DREAM_PATH . 'application/' . $this->module . '/view/' . $this->controller . '/' . $view . '.html';
		if (file_exists($viewFile)) {
			include $viewFile;
		}else {
			throw new \Exception('模板文件' . $viewFile . '不存在！');
		}
			
	}
}